package test

import java.text.SimpleDateFormat
import java.util.Calendar


import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession

object test {
  def main(args: Array[String]): Unit = {
//    val conf: SparkConf = new SparkConf().setAppName("test").setMaster("local")
//    val spark: SparkSession = SparkSession.builder().config(conf).getOrCreate()
//
//
//    import spark.implicits._
//
//    val word: RDD[String] = spark.sparkContext.textFile("/Users/denyoshiminogi/IdeaProjects/spark-etl/src/main/scala/test/word")
//    word.foreach(println)
//
//
//    val textSplit: RDD[String] = word.flatMap(_.split(" "))
//
//    val textSplitFlag: RDD[(String, Int)] = textSplit.map(word => (word,1))
//
//    val countWord: RDD[(String, Int)] = textSplitFlag.reduceByKey((x,y)=>x+y)
//
//    countWord.foreach(println)
    var  dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd")
    val cal: Calendar = Calendar.getInstance()
    cal.add(Calendar.DATE,-1)
    val yesterday: String = dateFormat.format(cal.getTime())
    print(yesterday)
    val tmp_a =
      """
        SELECT
          T.event_subject ,
          T.event_time,
          t.user_type,
          case when t2.event_subject is null  then  1 else 0 end as flag
         FROM
        (
               SELECT
                  distinct
                  t.user_id as event_subject ,
                  t.dt as event_time ,
                  t3.user_status as user_type
               from
                  dws.dws_event_user_active t
               left join dim.dim_user_affiliated_info t3 on t.user_id =t3.user_id
               where   t.event_time >= t3.start_time and   t.event_time <= t3.end_time
                      and   t.dt = '$dt'
             and t.source_system IN ('PC','WAP') --PC_M活跃的 人

          )T   -- pcm站 当天活跃的人数
          LEFT JOIN
          (

           SELECT DISTINCT   t.user_id as event_subject  from
               dws.dws_event_user_active t where dt<'2020-09-16'    and t.dt >='2019-08-01'
             and t.source_system ='APP'
           )t2
           on t.event_subject = t2.event_subject
        """.replace("$dt",yesterday)
      print(tmp_a)


  }
}
